package cn.qin.xhangapi.controller.auth;

import cn.qin.xhangapi.pojo.SysMenu;
import cn.qin.xhangapi.service.SysMenuService;
import cn.qin.xhangapi.util.auth.SysMenuUtil;
import cn.qin.xhangapi.util.response.ResponseResult;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RequestMapping("/api/menu")
@RestController
public class SysMenuController {
    @Resource
    private SysMenuService sysMenuService;

    /**
     * 获取系统用户权限菜单列表数据
     *
     * @return cn.duozai.ct.utils.response.ResponseResult
     * @author 多仔ヾ
     */
    @GetMapping("/getList")
    public ResponseResult getList() {
        QueryWrapper<SysMenu> queryWrapper = new QueryWrapper<>();
        // 根据sort_id倒序排列
        queryWrapper.orderByAsc("sort_id");
        List<SysMenu> sysMenuList = sysMenuService.list(queryWrapper);
        return ResponseResult.success(sysMenuList);
    }

    /**
     * 获取系统用户权限菜单树形列表数据
     *
     * @return cn.duozai.ct.utils.response.ResponseResult
     * @author 多仔ヾ
     */
    @GetMapping("/getTreeList")
    public ResponseResult getTreeList() {
        QueryWrapper<SysMenu> queryWrapper = new QueryWrapper<>();
        // 根据sort_id倒序排列
        queryWrapper.orderByAsc("sort_id");
        List<SysMenu> sysMenuList = sysMenuService.list(queryWrapper);
        // 调用工具类生成树形菜单
        List<SysMenu> treeList = SysMenuUtil.buildTreeMenu(sysMenuList);
        return ResponseResult.success(treeList);
    }

    /**
     * 添加系统用户权限菜单
     *
     * @param parentId    上级权限菜单id
     * @param name        权限菜单名称
     * @param description 权限菜单描述
     * @param perms       权限菜单标识
     * @param router      权限菜单访问路径
     * @param component   路由组件路径
     * @param type        权限菜单类型
     * @param sortId      排序id
     * @param isPublic    是否公共权限菜单
     * @param status      权限菜单状态
     * @return cn.duozai.ct.utils.response.ResponseResult
     * @author 多仔ヾ
     */
    @PostMapping("/add")
    public ResponseResult add(@RequestParam Long parentId,
                              @RequestParam String name,
                              @RequestParam String description,
                              @RequestParam String perms,
                              @RequestParam(required = false) String router,
                              @RequestParam(required = false) String component,
                              @RequestParam Integer type,
                              @RequestParam Integer sortId,
                              @RequestParam Integer isPublic,
                              @RequestParam Integer status) {

        SysMenu sysMenu = new SysMenu();
        sysMenu.setParentId(parentId);
        sysMenu.setName(name);
        sysMenu.setDescription(description);
        sysMenu.setPerms(perms);
        sysMenu.setRouter(router);
        sysMenu.setComponent(component);
        sysMenu.setType(type);
        sysMenu.setSortId(sortId);
        sysMenu.setIsPublic(isPublic);
        sysMenu.setStatus(status);

        if (sysMenuService.save(sysMenu)) {
            return ResponseResult.success("添加成功");
        } else {
            return ResponseResult.failure("添加失败");
        }
    }

    //根据id获取
    @GetMapping("/getById/{id}")
    public ResponseResult getById(@PathVariable Long id) {
        SysMenu sysMenu = sysMenuService.getById(id);
        return ResponseResult.success(sysMenu);
    }

    @PutMapping("/update")
    public ResponseResult update(@RequestParam Long id,
                                 @RequestParam Long parentId,
                                 @RequestParam String name,
                                 @RequestParam String description,
                                 @RequestParam String perms,
                                 @RequestParam(required = false) String router,
                                 @RequestParam(required = false) String component,
                                 @RequestParam Integer type,
                                 @RequestParam Integer sortId,
                                 @RequestParam Integer isPublic,
                                 @RequestParam Integer status) {

        SysMenu sysMenu = new SysMenu();
        sysMenu.setId(id);
        sysMenu.setParentId(parentId);
        sysMenu.setName(name);
        sysMenu.setDescription(description);
        sysMenu.setPerms(perms);
        sysMenu.setRouter(router);
        sysMenu.setComponent(component);
        sysMenu.setType(type);
        sysMenu.setSortId(sortId);
        sysMenu.setIsPublic(isPublic);
        sysMenu.setStatus(status);

        if (sysMenuService.updateById(sysMenu)) {
            return ResponseResult.success("修改成功");
        } else {
            return ResponseResult.failure("修改失败");
        }
    }

    //根据id删除
    @DeleteMapping("/delete/{id}")
    public ResponseResult delete(@PathVariable Long id) {
        List<SysMenu> sysMenuList = sysMenuService.list();
        // 调用工具类生成树形菜单
        List<SysMenu> treeList = SysMenuUtil.buildTreeMenu(sysMenuList);
        //判断当前id菜单下是否有子菜单，如果有则不能删除
        for (SysMenu sysMenu : treeList) {
            if (sysMenu.getId().equals(id)) {
                if (!sysMenu.getChildren().isEmpty()) {
                    return ResponseResult.failure("当前菜单下有子菜单，不能删除");
                }
            }
        }
        if (sysMenuService.removeById(id)) {
            return ResponseResult.success("删除成功");
        } else {
            return ResponseResult.failure("删除失败");
        }
    }
}
